package com.ruicar.afs.cloud.manage.creditchange.service.impl;


import cfca.paperless.client.util.StringUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lowagie.text.DocumentException;
import com.ruicar.afs.cloud.bizcommon.enums.TemplatePrintType;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormManage;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormManageService;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.modules.contract.enums.ProcessStatusEnum;
import com.ruicar.afs.cloud.image.config.FileProperties;
import com.ruicar.afs.cloud.image.config.GwtFastdfsProperties;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import com.ruicar.afs.cloud.image.store.enums.IsViewEnum;
import com.ruicar.afs.cloud.image.store.feign.ImageStoreApiFeign;
import com.ruicar.afs.cloud.image.vo.UploadInputVo;
import com.ruicar.afs.cloud.manage.common.entity.ComAttachementFile;
import com.ruicar.afs.cloud.manage.common.entity.ManageBankCardChange;
import com.ruicar.afs.cloud.manage.common.mapper.ComAttachementFileMapper;
import com.ruicar.afs.cloud.manage.common.service.ComAttachementFileService;
import com.ruicar.afs.cloud.manage.common.service.ManageBankCardChangeService;
import com.ruicar.afs.cloud.manage.creditchange.enums.BankTemplateIdEnum;
import com.ruicar.afs.cloud.manage.creditchange.enums.BankTemplatePrintTypeEnum;
import com.ruicar.afs.cloud.manage.creditchange.service.ContractTemplateService;
import com.ruicar.afs.cloud.manage.creditchange.util.PrintToPdfUtil;
import com.ruicar.afs.cloud.manage.creditchange.vo.*;
import groovy.util.logging.Slf4j;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author chuke
 * @create 2020/8/11 16:31
 */
@Slf4j
@Service
@AllArgsConstructor
public class ContractTemplateServiceImpl implements ContractTemplateService {
    private final ComPrintFormManageService comPrintFormManageService;
    private final ManageBankCardChangeService bankCardChangeService;
    private final ComAttachmentFileService comAttachmentFileService;
    private final ComAttachementFileMapper comAttachementFileMapper;
    private final GwtFastdfsProperties fastdfsProperties;
    private final ImageStoreApiFeign imageStoreApiFeign;
    private final FileProperties fileProperties;
    private final ComAttachementFileService comAttachementFileService;


    /**
     * 根据原数据进行模板打印删除，授权书
     *
     * @param bankCardManageVO
     * @return
     */
    @Override
    public void contractTemplate(HttpServletResponse response, BankCardManageVO bankCardManageVO) throws ParseException {
        if (!(bankCardManageVO.getTypeEnum()).equals(BankTemplatePrintTypeEnum.PERMIT.name())) {

            //获取模板id列表
            BankTemplateVO vo = getTemplateList(bankCardManageVO);
            //模板数据VO
            TemplateContractVO templateVO = new TemplateContractVO();//模板Vo
            ManageBankCardChange bankCard = bankCardChangeService.getOne(Wrappers.<ManageBankCardChange>lambdaQuery().eq(ManageBankCardChange::getCaseNo, vo.getCaseNo()));
            templateVO.setCertNo(bankCard.getCertNo());//证件号
            templateVO.setCustName(bankCard.getCustName());//客户名
            templateVO.setApplyNo(bankCard.getApplyNo());
            templateVO.setNewBankAccount(bankCard.getNewBankAccount());
            templateVO.setNewCardholder(bankCard.getNewCardholder());
            /*工商银行新增模板不需要原账户信息*/
           if (bankCard.getOldBank().equals(bankCard.getNewBank())) {
                //工商与建设银行，同行变更要填写原账户与账号
                templateVO.setOldBank(bankCard.getOldBank());//原银行
                templateVO.setOldBankAccount(bankCard.getOldBankAccount());//账户号
                templateVO.setOldCardholder(bankCard.getOldCardholder());//账号开户名
            }
            templateVO.setSignTime(DatePattern.CHINESE_DATE_FORMAT.format(new Date())); //签署时间
            //合同关闭时间+三个月
            if (bankCard.getCloseDate() != null) {
                Calendar endDate = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String timeFormat = sdf.format(bankCard.getCloseDate());
                Date endDatetime = sdf.parse(timeFormat);
                endDate.setTime(endDatetime);
                endDate.add(Calendar.MONTH, 3);
                String defaultEndDate = sdf.format(endDate.getTime());
                templateVO.setCloseDate(DatePattern.CHINESE_DATE_FORMAT.format(sdf.parse(defaultEndDate)));
            }
//        templateVO.setEndDate();//有效期截至（合同关闭时间+三个月）
            // 生成模板
            JSONObject jsonTemp = (JSONObject) JSONObject.toJSON(templateVO);

            //删除原来存在的附件，生成新的附件
            comAttachmentFileService.remove(Wrappers.<ComAttachmentFile>query().lambda()
                    .eq(ComAttachmentFile::getBusiNo, vo.getCaseNo())
                    .eq(ComAttachmentFile::getFileType, "PDF")
                    .eq(ComAttachmentFile::getFileStatus, "standard"));
            comPrintFormManageService.print(jsonTemp, bankCardManageVO.getTemplateId(), vo.getCaseNo(), vo.getContractNo());
        } else { //用申请资料，身份证正反照，银行卡正反照合成授权书
            //身份证与银行卡照片附件list
            List<String> pictureList = new ArrayList();
            //获取模板id列表
            BankTemplateVO vo = getTemplateList(bankCardManageVO);
            pictureList.add(vo.getBankPhotoDownId().toString());
            pictureList.add(vo.getBankPhotoUpId().toString());
            pictureList.add(vo.getIdentityPhotoDownId().toString());
            pictureList.add(vo.getIdentityPhotoUpId().toString());
            ComAttachementFile permitCode = comAttachementFileService.getOne(Wrappers.<ComAttachementFile>lambdaQuery()
                    .eq(ComAttachementFile::getBusiNo, bankCardManageVO.getCaseNo())
                    .eq(ComAttachementFile::getAttachmentCode, "mergeFile"));
            //当文件不为合成文件时，对文件进行合成
            if (permitCode != null
                    && (permitCode.getRemake() == null
                    || !permitCode.getRemake().equals("merge"))) {
                pictureList.add(String.valueOf(permitCode.getId()));
                mergeContractFile(response, pictureList, bankCardManageVO);
            }
        }
    }

    /**
     * 根据原数据进行模板打印新增模板
     *
     * @param approvalVo
     * @return
     */
    @Override
    public void businessTemplate(BankApprovalVo approvalVo) {
        List<BankCardManageVO> vos = approvalVo.getBankCardManageVOs();
        List<ManageBankCardChange> bankCardChangeList = new ArrayList<>();
        int templateNo = 1;
        //获取模板id列表
        BankTemplateVO vo = getTemplateList(vos.get(0));
        //模板数据VO
        TemplateContractVO templateVO = new TemplateContractVO();
        for (BankCardManageVO bankCardManageVO : vos) {
            ManageBankCardChange bankCard = bankCardChangeService.getOne(Wrappers.<ManageBankCardChange>lambdaQuery()
                    .eq(ManageBankCardChange::getCaseNo, bankCardManageVO.getCaseNo())
                    );
            if (!bankCard.getOldBank().equals(bankCard.getNewBank())) {
                bankCard.setOldBankAccount("");
            }

            bankCard.setTemplateNo(String.valueOf(templateNo));
            templateVO.setSignTime(DatePattern.CHINESE_DATE_FORMAT.format(new Date()));
            bankCardChangeList.add(bankCard);
            templateNo ++;
        }
        templateVO.setBankChangeList(bankCardChangeList);
        JSONObject jsonTemp = (JSONObject) JSONObject.toJSON(templateVO);

        if (approvalVo.getTypeEnum().equals(BankTemplatePrintTypeEnum.DETAIL.name())) {
            //删除记录的模板，防止下载到历史脏数据
            comAttachmentFileService.remove(Wrappers.<ComAttachmentFile>query().lambda()
                    .eq(ComAttachmentFile::getBusiNo, vo.getCaseNo())
                    .eq(ComAttachmentFile::getAttachmentCode, approvalVo.getTemplateId())
                    .eq(ComAttachmentFile::getFileType, "PDF"));
            comPrintFormManageService.print(jsonTemp, approvalVo.getTemplateId(), vo.getCaseNo(), vo.getContractNo());
        } else {
            //删除记录的模板，防止下载到历史脏数据
            comAttachmentFileService.remove(Wrappers.<ComAttachmentFile>query().lambda()
                    .eq(ComAttachmentFile::getBusiNo, vo.getCaseNo())
                    .eq(ComAttachmentFile::getAttachmentCode, approvalVo.getTemplateId())
                    .eq(ComAttachmentFile::getFileType, "PDF"));
            comPrintFormManageService.print(jsonTemp, approvalVo.getTemplateId(), vo.getCaseNo(), vo.getContractNo());
        }

    }

    @Override
    public ComAttachementFile printPermitFile(BankCardManageVO vo) throws ParseException {
        //模板数据VO
        TemplateContractVO templateVO = new TemplateContractVO();//模板Vo
        ManageBankCardChange bankCard = bankCardChangeService.getOne(Wrappers.<ManageBankCardChange>lambdaQuery()
                .eq(ManageBankCardChange::getCaseNo, vo.getCaseNo()));
        templateVO.setCertNo(bankCard.getCertNo());//证件号
        templateVO.setCustName(bankCard.getCustName());//客户名
        if (bankCard.getOldBank().equals(bankCard.getNewBank())) {
            //工商与建设银行，同行变更要填写原账户与账号
            templateVO.setOldBank(bankCard.getOldBank());//原银行
            templateVO.setOldBankAccount(bankCard.getOldBankAccount());//账户号
            templateVO.setOldCardholder(bankCard.getOldCardholder());//账号开户名
        }
        templateVO.setNewBank(bankCard.getNewBank());//新银行
        templateVO.setNewBankAccount(bankCard.getNewBankAccount());//新银行账户
        templateVO.setNewCardholder(bankCard.getNewCardholder());//新持卡人
        templateVO.setCaseNo(bankCard.getCaseNo());//新持卡人
        templateVO.setSignTime(DatePattern.CHINESE_DATE_FORMAT.format(new Date())); //签署时间
        //使用basicMainInfoFeign多返回一个合同关闭时间
//        templateVO.setEndDate();//有效期截至（合同关闭时间+三个月）
        //合同关闭时间+三个月
        if (bankCard.getCloseDate() != null) {
            Calendar endDate = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String timeFormat = sdf.format(bankCard.getCloseDate());
            Date endDatetime = sdf.parse(timeFormat);
            endDate.setTime(endDatetime);
            endDate.add(Calendar.MONTH, 3);
            String defaultEndDate = sdf.format(endDate.getTime());
            templateVO.setCloseDate(DatePattern.CHINESE_DATE_FORMAT.format(sdf.parse(defaultEndDate)));
        }
        // 生成模板
        JSONObject jsonTemp = (JSONObject) JSONObject.toJSON(templateVO);
        comPrintFormManageService.print(jsonTemp, vo.getTemplateId(), vo.getCaseNo(), vo.getContractNo());
        ComAttachementFile attachementFile = comAttachementFileService.getOne(Wrappers.<ComAttachementFile>lambdaQuery()
                .eq(ComAttachementFile::getBusiNo, bankCard.getCaseNo())
                .eq(ComAttachementFile::getFileType, "PDF"));
        return attachementFile;
    }

    @Override
    public ComAttachementFile mergeUploadFile(HttpServletResponse response, BankCardManageVO bankCardManageVO) {
        //身份证与银行卡照片附件list
        List<String> pictureList = new ArrayList();
        //查询并删除之前生成的模板
        List<ComAttachementFile> delCode = comAttachementFileService.list(Wrappers.<ComAttachementFile>lambdaQuery()
                .eq(ComAttachementFile::getBusiNo, bankCardManageVO.getCaseNo())
                .eq(ComAttachementFile::getAttachmentCode, "mergeFile"));
        if (delCode.size() != 0) {
            for (ComAttachementFile attachementFile : delCode) {
                comAttachementFileService.removeById(attachementFile.getId());
            }
        }
        //获取模板id列表
        BankTemplateVO vo = getTemplateList(bankCardManageVO);
        pictureList.add(vo.getBankPhotoDownId().toString());
        pictureList.add(vo.getBankPhotoUpId().toString());
        pictureList.add(vo.getIdentityPhotoDownId().toString());
        pictureList.add(vo.getIdentityPhotoUpId().toString());
        ComAttachementFile permitCode = comAttachementFileService.getOne(Wrappers.<ComAttachementFile>lambdaQuery()
                .eq(ComAttachementFile::getBusiNo, bankCardManageVO.getCaseNo())
                .eq(ComAttachementFile::getAttachmentCode, "4588631527359512579")
                .eq(ComAttachementFile::getFileStatus, "draft"));
        pictureList.add(String.valueOf(permitCode.getId()));
        mergeContractFile(response, pictureList, bankCardManageVO);
        ComAttachementFile mergeFile = comAttachementFileService.getOne(Wrappers.<ComAttachementFile>lambdaQuery()
                .eq(ComAttachementFile::getBusiNo, bankCardManageVO.getCaseNo())
                .eq(ComAttachementFile::getAttachmentCode, "mergeFile")
                .eq(ComAttachementFile::getFileStatus, "standard"));
        //标注为合成文件
        mergeFile.setRemake("merge");
        comAttachementFileService.updateById(mergeFile);
        return mergeFile;
    }

    /**
     * 获取模板列表
     * ComAttachmentCondition
     *
     * @param
     * @return
     */
    @Override
    public List<ComAttachmentFile> getComAttachList(String attachmentCode, List caseNoList) {
        List<ComAttachmentFile> list = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                .eq(StringUtil.isNotEmpty(attachmentCode), ComAttachmentFile::getAttachmentCode, attachmentCode)
                .eq(ComAttachmentFile::getFileType, "PDF")
                .in(caseNoList.size() != 0, ComAttachmentFile::getBusiNo, caseNoList));
        return list;
    }

    /**
     * 获取下载类型
     *
     * @param manageVOList
     * @return
     */
    @Override
    public String photoTemplate(BankApprovalVo manageVOList) {
        String bankCode = manageVOList.getBankCardManageVOs().get(0).getNewBank();
        //代扣银行不一致时，不允许进行生成
        for (BankCardManageVO bankCardManageVO : manageVOList.getBankCardManageVOs()) {
            if (!bankCardManageVO.getNewBank().equals(bankCode)) {
                throw new AfsBaseException("所选列中存在不同代扣银行，无法生成模板");
            }
        }
        String template = "";
        List<BankCardManageVO> bankList = manageVOList.getBankCardManageVOs();
        //下载删除列表
        if (manageVOList.getTypeEnum().equals(BankTemplatePrintTypeEnum.DROP.name())) {
            bankList.forEach(list -> {
                if (!list.getOldBank().equals(list.getNewBank())) {
                    throw new AfsBaseException("存在不是同行变更的记录，无法下载删除列表");
                }
            });
            switch (bankList.get(0).getOldBank()) {
                case "137"://工商银行
                    ComPrintFormManage icbcId = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>lambdaQuery()
                            .eq(ComPrintFormManage::getPrintFormId, "ICBC002"));
                    template = String.valueOf(icbcId.getId());
                    break;
                //TODO CK 农业银行信息添加
                case "524853"://中国建设银行
                    ComPrintFormManage ccbId = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>lambdaQuery()
                            .eq(ComPrintFormManage::getPrintFormId, "CCB004"));
                    template = String.valueOf(ccbId.getId());
                    break;
                default:
                    throw new AfsBaseException("该银行没有删除模板");

            }
        } else if (manageVOList.getTypeEnum().equals(BankTemplatePrintTypeEnum.INCREASE.name())) {
            switch (bankList.get(0).getNewBank()) {
                case "137"://工商银行
                    ComPrintFormManage icbcId = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>lambdaQuery()
                            .eq(ComPrintFormManage::getPrintFormId, "ICBC003"));
                    template = String.valueOf(icbcId.getId());
                    break;
                case "101"://中国银行
                    ComPrintFormManage bocId = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>lambdaQuery()
                            .eq(ComPrintFormManage::getPrintFormId, "BOC001"));
                    template = String.valueOf(bocId.getId());
                    break;
                case "524853"://中国建设银行
                    ComPrintFormManage ccbId = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>lambdaQuery()
                            .eq(ComPrintFormManage::getPrintFormId, "CCB001"));
                    template = String.valueOf(ccbId.getId());
                    break;
                default:
                    throw new AfsBaseException("该银行没有新增模板");
            }
        } else if (manageVOList.getTypeEnum().equals(BankTemplatePrintTypeEnum.DETAIL.name())) {
            switch (bankList.get(0).getNewBank()) {
                case "137"://工商银行
                    ComPrintFormManage icbcId = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>lambdaQuery()
                            .eq(ComPrintFormManage::getPrintFormId, "ICBC004"));
                    template = String.valueOf(icbcId.getId());
                    break;
                case "101"://中国银行
                    ComPrintFormManage bocId = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>lambdaQuery()
                            .eq(ComPrintFormManage::getPrintFormId, "BOC003"));
                    template = String.valueOf(bocId.getId());
                    break;
                case "524853"://中国建设银行
                    ComPrintFormManage ccbId = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>lambdaQuery()
                            .eq(ComPrintFormManage::getPrintFormId, "CCB003"));
                    template = String.valueOf(ccbId.getId());
                    break;
                case "5500814"://中国邮储银行
                    ComPrintFormManage psbcId = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>lambdaQuery()
                            .eq(ComPrintFormManage::getPrintFormId, "PSBC002"));
                    template = String.valueOf(psbcId.getId());
                    break;
                default:
                    break;
            }
        } else {
            template = BankTemplateIdEnum.bankCardPermitTemplate.getDesc();

        }
        return template;
    }


    //获取身份证正反照，银行卡正反照id
    public BankTemplateVO getTemplateList(BankCardManageVO bankCardManageVO) {

        BankTemplateVO vo = new BankTemplateVO();
        vo.setIdCardFrontCode(BankTemplateIdEnum.BankChangeIdCardFrontCode.getDesc());
        vo.setIdCardBackCode(BankTemplateIdEnum.BankChangeIdCardBackCode.getDesc());
        vo.setBankCardFrontCode(BankTemplateIdEnum.BankChangeBankCardFrontCode.getDesc());
        vo.setBankCardBackCode(BankTemplateIdEnum.BankChangeBankCardBackCode.getDesc());
        ComAttachementFile idCardFrontFile = comAttachementFileMapper.selectOne(Wrappers.<ComAttachementFile>lambdaQuery()
                .eq(ComAttachementFile::getBusiNo, bankCardManageVO.getCaseNo())
                .eq(ComAttachementFile::getAttachmentCode, vo.getIdCardFrontCode()));
        ComAttachementFile idCardBackFile = comAttachementFileMapper.selectOne(Wrappers.<ComAttachementFile>lambdaQuery()
                .eq(ComAttachementFile::getBusiNo, bankCardManageVO.getCaseNo())
                .eq(ComAttachementFile::getAttachmentCode, vo.getIdCardBackCode()));
        ComAttachementFile bankFrontFile = comAttachementFileMapper.selectOne(Wrappers.<ComAttachementFile>lambdaQuery()
                .eq(ComAttachementFile::getBusiNo, bankCardManageVO.getCaseNo())
                .eq(ComAttachementFile::getAttachmentCode, vo.getBankCardFrontCode()));
        ComAttachementFile bankBackFile = comAttachementFileMapper.selectOne(Wrappers.<ComAttachementFile>lambdaQuery()
                .eq(ComAttachementFile::getBusiNo, bankCardManageVO.getCaseNo())
                .eq(ComAttachementFile::getAttachmentCode, vo.getBankCardBackCode()));
        //身份证正反照ID
        if (idCardFrontFile == null || idCardBackFile == null) {
            throw new AfsBaseException("缺少身份证正反照");
        } else {
            vo.setIdentityPhotoUpId(idCardFrontFile.getId());
            vo.setIdentityPhotoDownId(idCardBackFile.getId());
        }
        //银行卡正反照ID
        if (bankBackFile == null || bankFrontFile == null) {
            throw new AfsBaseException("缺少银行卡正反照");
        } else {
            vo.setBankPhotoUpId(bankFrontFile.getId());
            vo.setBankPhotoDownId(bankBackFile.getId());
        }
        vo.setCaseNo(bankCardManageVO.getCaseNo());
        vo.setContractNo(bankCardManageVO.getContractNo());
        return vo;
    }

    /**
     * 合成身份证，银行卡照片与授权书文件为pdf
     *
     * @param response
     * @param fileList
     */
    public void mergeContractFile(HttpServletResponse response, @RequestBody List<String> fileList, BankCardManageVO bankCardManageVO) {
        List<ComAttachementFile> attachementFiles = comAttachementFileService.listByIds(fileList);
        //创建临时文件
        String fileImageName = File.separator + DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumber() + File.separator;
        String destPath = fileProperties.getTempDir() + File.separator + DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumber() + File.separator;
        String fileNamePdf = File.separator + DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumber() + File.separator;
        String fileName = null;

        for (ComAttachementFile attachementFile : attachementFiles) {
            if ("pdf".equals(attachementFile.getFileType()) || "PDF".equals(attachementFile.getFileType())) {
                fileName = fileNamePdf;
            } else {
                fileName = fileImageName;
            }
            // 2. 从远端接口获取数据
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", comAttachmentFileService.getAuthorization());
            byte[] fileByte = imageStoreApiFeign.downFile(attachementFile.getFileId(), fastdfsProperties.getSceneId(), attachementFile.getFileName(), IsViewEnum.download.getCode(), null, headers);
            if (fileByte.length > 0) {
                FileUtil.writeBytes(fileByte, destPath + File.separator + fileName + attachementFile.getFileName());
            }
        }
        //合并图片
        try {
            PrintToPdfUtil.toPdf(destPath + File.separator + fileImageName, destPath + File.separator + fileNamePdf);

            String filePdf = File.separator + DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumber() + File.separator;
            //合并pdf
            PermitPdfInfoVO permitPdfInfoVO = PrintToPdfUtil.PDFMerger(destPath + File.separator + fileNamePdf, destPath + File.separator + filePdf);
            File outFile = FileUtil.touch(permitPdfInfoVO.getFileName());
            //文件名称
            String pdfFileName = bankCardManageVO.getCaseNo() + "_" + bankCardManageVO.getCustName() + ".PDF";
            //查出原文件
            ComAttachementFile permitFile = comAttachementFileService.getOne(Wrappers.<ComAttachementFile>lambdaQuery()
                    .eq(ComAttachementFile::getAttachmentCode, bankCardManageVO.getTemplateId())
                    .eq(ComAttachementFile::getBusiNo, bankCardManageVO.getCaseNo())
                    .eq(ComAttachementFile::getFileStatus, "standard"));
            //删除原文件
            if (permitFile != null) {
                comAttachementFileService.removeById(permitFile.getId());
            }
            UploadInputVo vo = new UploadInputVo();
            vo.setBusiNo(bankCardManageVO.getCaseNo());//业务编号
            vo.setBelongNo(bankCardManageVO.getContractNo());//模板id
            vo.setAttachmentName(pdfFileName);//文件名称
            vo.setAttachmentId("mergeFile");
            vo.setFileName(pdfFileName);
            vo.setFileType(TemplatePrintType.PDF.name());
            vo.setFileSource("bankCard");
            comAttachmentFileService.storeFileToOss(outFile, vo);
            FileUtil.del(outFile);  //上传完成后，删除本地文件
        } catch (IOException e) {
            e.printStackTrace();
            // FileUtil.del(destPath);  //上传完成后，删除本地文件
        } catch (DocumentException e) {
            e.printStackTrace();
//           FileUtil.del(destPath);
        }

    }
}



